package omp.syst.eng;

import java.awt.image.*;

public class Enhancer {

	public static int TYPE_NEG_TRANSFORM = 0;
	public static int TYPE_LOG_TRANSFORM = 1;
	public static int TYPE_POW_TRANSFORM = 2;
	public static int TYPE_GSL_TRANSFORM = 3;
	public static int TYPE_HST_TRANSFORM = 4;
	
	private BufferedImage img;
	private int lookup[];
	public Enhancer(){}
	
	public void setImage( BufferedImage argImage ){
		
		//int w = img.getWidth(), h = img.getHeight();
		
		this.img = argImage;
		/*
		 * 
		for( int i = 0; i < w; i++ )
			for( int j = 0; j < h; j++ )
				pixels[ i ][ j ] = new Pixel( img.getRGB( i, j ) );
		 */
		
	}
	
	public BufferedImage getImage(){
		
		return this.img;
	}
	
	public void enhance( int p ){
		
		switch( p ){
		
		case 0:
			negTransform();
			break;
			
		case 1:
			logTransform( 3 );
			break;
			
		case 2:
			//powTransform();
			break;
			
		case 3:
			grayScaleTransform();
			break;
			
		case 4:
			histogramTransform();
			break;

		default:
				//powTranform();
				break;
		}
	}
	
	private void negTransform(){
		
		int w = img.getWidth(), h = img.getHeight();
		
		for( int i = 0; i < w; i++ )
			for( int j = 0; j < h; j++ )
				img.setRGB( i, j, ~img.getRGB( i, j ) );
		
	}
	
	private void logTransform( int c ){
		/*
		 * 
		int w = img.getWidth(), h = img.getHeight();
		Pixel pixel;
		
		for( int i = 0; i < w; i++ )
			for( int j = 0; j < h; j++ ){
				
				pixel = new Pixel( img.getRGB( i, j ) );
				
				pixel.setRed( c * Integer.parseInt( Double.toString( Math.log( 1 + pixel.getRed() ) ) ) );
				pixel.setGreen( c * Integer.parseInt( Double.toString( Math.log( 1 + pixel.getGreen() ) ) ) );
				pixel.setBlue( c * Integer.parseInt( Double.toString( Math.log( 1 + pixel.getBlue() ) ) ) );
				
				img.setRGB( i, j, pixel.getPixel() );
			}
		 */
	}
	
	private void grayScaleTransform( ){
		
		int w = img.getWidth(), h = img.getHeight();
		Pixel p;
		
		for( int i = 0; i < w; i++ )
			for( int j = 0; j < h; j++ ){
				
				p = new Pixel( img.getRGB( i, j ) );
				p.setGrayScale();
				img.setRGB( i, j, p.getPixel() );
			}
	}
	
	private void histogramTransform(){
		
		int m = img.getWidth(), n = img.getHeight(), count;
		lookup = new int[ m * n ];
		Pixel p;
		
		grayScaleTransform();
		
		for( int level = 0; level <= 255; level++ ){

			count = 0;
			
			for( int i = 0; i < m; i++ )
				for( int j = 0; j < n; j++ ){
					
					p = new Pixel( img.getRGB( i, j ) );
					if( level == p.getRed() )
						count++;
				}
			
			count /= ( m * n );
			
			lookup[ level ] = count * 255;
		}
			
		for( int i = 0; i < m; i++ )
			for( int j = 0; j < n; j++ ){
				
				p = new Pixel( img.getRGB( i, j ) );
				img.setRGB( i, j, lookup[ p.getRed() ] );
			}
	}
}
